package com.yuan.mint.activity;

import android.os.Bundle;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.yuan.mint.R;
import com.yuan.mint.adapter.RecommendDetailAdapter;
import com.yuan.mint.bean.DietBaseBean;
import com.yuan.mint.bean.DietPostsBean;
import com.yuan.mint.bean.DietTopicBean;
import com.yuan.mint.fragment.NearbyFragment;
import com.yuan.mint.fragment.RecommendFragment;
import com.yuan.mint.utils.AppConstant;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

public class RecommendDetailActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2<ListView> {

    private String uri;
    private PullToRefreshListView pullListView;
    private RecommendDetailAdapter adapter;
    private ArrayList<DietPostsBean> posts = new ArrayList<>();
    private DietTopicBean topicBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diet);
        initUrl();
        initListView();
        initData();
    }

    private void initUrl() {
        String name = getIntent().getStringExtra("name");
        if (RecommendFragment.DIET_NAME.equals(name)) {
            uri = AppConstant.HEALTHY_DIET;
        } else if (RecommendFragment.EXERCISE_NAME.equals(name)) {
            uri = AppConstant.SHARE_EXERCISE;
        } else if (NearbyFragment.NEWEST_NAME.equals(name)) {
            uri = AppConstant.NEARBY_NEWEST;
        } else if (NearbyFragment.SELECTED_NAME.equals(name)) {
            uri = AppConstant.NEARBY_SELECTED;
        } else if (NearbyFragment.HOTSPOT_NAME.equals(name)) {
            uri = AppConstant.NEARBY_HOTSPOT;
        }
    }

    private void initData() {
        AsyncHttpClient httpClient = new AsyncHttpClient();
        final TypeReference<DietBaseBean<DietPostsBean>> type = new TypeReference<DietBaseBean<DietPostsBean>>() {
        };
        httpClient.get(this, uri, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if (responseString != null) {
                    DietBaseBean<DietPostsBean> baseBean = JSONObject.parseObject(responseString, type);
                    posts.addAll(baseBean.getPosts());
                    topicBean = baseBean.getTopic();
                    adapter.notifyDataSetChanged();
                    pullListView.onRefreshComplete();
                }
            }
        });

    }

    private void initListView() {
        pullListView = (PullToRefreshListView) findViewById(R.id.diet_health_list);
        pullListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullListView.setOnRefreshListener(this);
        adapter = new RecommendDetailAdapter(posts, this);
        pullListView.setAdapter(adapter);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

    }
}
